import { ApiProperty } from '@nestjs/swagger'

export class ApiResp<T> {
  @ApiProperty({
    description: '错误提示信息',
  })
  message?: string

  @ApiProperty({
    description: '业务数据',
  })
  data?: T

  @ApiProperty({
    example: 200,
    description: '业务状态码',
  })
  statusCode: number
  @ApiProperty({
    example: 200,
    description: '响应时间',
  })
  date: Date
  @ApiProperty({
    example: 200,
    description: '异常时的请求资源路径',
  })
  path: string

  constructor({
    message,
    data,
    statusCode = 200,
    path,
  }: {
    message?: string
    data?: T
    statusCode?: number
    path?: string
  }) {
    this.message = message
    this.data = data
    this.statusCode = statusCode
    this.date = new Date()
    this.path = path
  }

  static success<T>(data?: T) {
    return new ApiResp<T>({ data })
  }
  static error(statusCode: number, message: string, path?: string) {
    return new ApiResp<undefined>({ statusCode, message, path })
  }
}
